const articleModel = require('../models/Article');
const logUtil = require('../../untils/logUtil');
const success = require('../success');
const error = require('../error');
const categoryModel = require('../models/Category');
//const Op = Sequelize.Op;

class Article {
    constructor() {
        // super()
    }

    //获取文章列表
    async getList(req, res, next) {
        try {
            /*let article = await articleModel.findAll({
                attributes: ['id', 'title','cate_id','create_time','writer','remark','photo','keyword','views','comment_count'],
                order:[['id','desc']]
            });*/
            let current = req.query.current || 1;
            let pageSize = req.query.pageSize || 10;
            let offset = (current - 1) * pageSize;
            let typeId = req.query.typeId; // 分类ID
            let searchkey = req.query.searchkey; // 搜索关键字
            let state = req.query.state;
            let article = await articleModel.findAndCountAll({

                where: {
                    status: 1
                },
                include: {
                    model: categoryModel,
                    as: 'category',
                    attributes: ['id', 'name']
                },
                attributes: ['id', 'title', 'cate_id', 'create_time', 'writer', 'remark', 'photo', 'keyword', 'views', 'comment_count'],
                order: [['id', 'desc']],
                offset: offset,
                limit: pageSize
            });
            res.json(
                success(article)
            );
        } catch (err) {
            logUtil.error(err, req);
            res.json(
                error(err.message)
            );
        }
    }

    //获取热门文章
    async getHots(req, res, next) {
        try {
            let datas = await articleModel.findAll({
                attributes: ['id', 'title', 'create_time', 'views'],
                limit: 8,
                order: [['views', 'desc'], ['id', 'desc']]

            });
            res.json(success(datas));

        } catch (err) {
            logUtil.error(err, req);
            error(err.message);
        }
    }

    //获取推荐文章
    async getRecs(req, res, next) {
        try {
            let datas = await articleModel.findAll({
                where: {is_tui: 1},
                attributes: ['id', 'title', 'create_time', 'views'],
                limit: 8,
                order: [['id', 'desc']]

            });
            res.json(success(datas));

        } catch (err) {
            logUtil.error(err, req);
            error(err.message);
        }
    }

    //获取详情
    async getOne(req, res, next) {
        try {
            let id = req.query.id;
            let info = await articleModel.findById(id, {
                attributes: ['id', 'title', 'cate_id', 'create_time', 'writer', 'remark', 'keyword', 'views', 'content']
            });
            res.json(success(info));

        } catch (err) {
            logUtil.error(err, req);
            error(err.message);
        }
    }

    //增加浏览数
    async addView(req, res, next) {
        try {
            await articleModel.increment('views', {where: {id: req.query.id}, by: 1},);
            res.json(success());
        } catch (err) {
            res.json(
                error(err.message)
            );
        }

    }
}

module.exports = new Article();
